package hu.uniobuda.nik.bsik2f.googlemapsv2demo;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MyActivity extends FragmentActivity {
    private GoogleMap map;
    private MarkerOverlay overlay;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        map = mapFragment.getMap();
        map.setMyLocationEnabled(true);
        map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
                map.addMarker(new MarkerOptions()
                        .position(new LatLng(location.getLatitude(), location.getLongitude()))
                        .title("Coordinate")
                        .draggable(false));
            }
        });

        overlay = new MarkerOverlay(map);
        map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
                LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
                overlay.addMarker(position, "TestMarker");
            }
        });


        Button btnMove = (Button) findViewById(R.id.buttonMove);
        Button btnZoom = (Button) findViewById(R.id.buttonZoom);
        Button btnCamera = (Button) findViewById(R.id.buttonCamera);

        btnMove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(47.539999, 19.039999)));
            }
        });

        btnZoom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                map.animateCamera(CameraUpdateFactory.zoomTo(17), 5000, null);
            }
        });

        btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                map.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
                        .target(new LatLng(19.000000, 47.5000))
                        .bearing(30)
                        .tilt(45)
                        .zoom(11)
                        .build()), 7000, new GoogleMap.CancelableCallback() {
                    @Override
                    public void onFinish() {
                        map.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
                                .target(new LatLng(47.539999, 19.039999))
                                .bearing(0)
                                .zoom(13)
                                .build()), 5000, null);
                    }

                    @Override
                    public void onCancel() {
                        // TODO
                    }
                });
            }
        });

    }
}
